// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <cstdint>
#include <memory>
#include <utility>


#include <nebula/types/type_fwd.h>
#include <nebula/util/type_fwd.h>
#include <turbo/base/macros.h>

namespace nebula::csv {

    class BlockParser;

    struct ConvertOptions;

    class TURBO_EXPORT ColumnBuilder {
    public:
        virtual ~ColumnBuilder() = default;

        /// Spawn a task that will try to convert and append the given CSV block.
        /// All calls to append() should happen on the same thread, otherwise
        /// call insert() instead.
        virtual void append(const std::shared_ptr<BlockParser> &parser) = 0;

        /// Spawn a task that will try to convert and insert the given CSV block
        virtual void insert(int64_t block_index,
                            const std::shared_ptr<BlockParser> &parser) = 0;

        /// Return the final chunked array.  The TaskGroup _must_ have finished!
        virtual turbo::Result<std::shared_ptr<ChunkedArray>> finish() = 0;

        std::shared_ptr<nebula::internal::TaskGroup> task_group() { return task_group_; }

        /// Construct a strictly-typed ColumnBuilder.
        static turbo::Result<std::shared_ptr<ColumnBuilder>> create(
                MemoryPool *pool, const std::shared_ptr<DataType> &type, int32_t col_index,
                const ConvertOptions &options,
                const std::shared_ptr<nebula::internal::TaskGroup> &task_group);

        /// Construct a type-inferring ColumnBuilder.
        static turbo::Result<std::shared_ptr<ColumnBuilder>> create(
                MemoryPool *pool, int32_t col_index, const ConvertOptions &options,
                const std::shared_ptr<nebula::internal::TaskGroup> &task_group);

        /// Construct a ColumnBuilder for a column of nulls
        /// (i.e. not present in the CSV file).
        static turbo::Result<std::shared_ptr<ColumnBuilder>> MakeNull(
                MemoryPool *pool, const std::shared_ptr<DataType> &type,
                const std::shared_ptr<nebula::internal::TaskGroup> &task_group);

    protected:
        explicit ColumnBuilder(std::shared_ptr<nebula::internal::TaskGroup> task_group)
                : task_group_(std::move(task_group)) {}

        std::shared_ptr<nebula::internal::TaskGroup> task_group_;
    };

}  // namespace nebula::csv
